<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="机构名" prop="no">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入机构名"
            clearable
            style="width: 150px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="商户名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入商户名称"
            clearable
            style="width: 200px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="状态"
            clearable
            style="width: 120px"
          >
            <el-option key="A" label="启用" value="A"/>
            <el-option key="N" label="禁用" value="N"/>
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['merchant:index']"
          >新增机构</el-button>
        </el-form-item>
      </el-form>
  
      <el-table ref="tables" v-loading="loading" :data="list" @selection-change="handleSelectionChange" :default-sort="defaultSort" @sort-change="handleSortChange">
        <el-table-column label="ID" prop="id"  width="55"/>
        <el-table-column label="机构名称" align="center" prop="name" />
        <el-table-column label="机构地址" align="center" prop="address" />
        <el-table-column label="负责人" align="center" prop="head"/>
        <el-table-column label="机构电话" align="center" prop="phone"/>
        <!-- <el-table-column label="机构简介" align="center" prop="intro"/>
        <el-table-column label="详情图片" align="center" width="200">
            <template slot-scope="scope">
                <img class="list-qr" :src="imagePath + scope.row.image">
            </template>
        </el-table-column> -->
        <el-table-column label="封面图片" align="center" width="200">
            <template slot-scope="scope">
                <img class="list-qr" :src="imagePath + scope.row.coverImage">
            </template>
        </el-table-column>
        <!-- <el-table-column label="创建时间" align="center" prop="createTime"/> -->
        <el-table-column label="创建时间" align="center" prop="createTime" sortable="custom" :sort-orders="['descending', 'ascending']" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-hasPermi="['merchant:index']"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              v-hasPermi="['merchant:index']"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改对话框 -->
      <el-dialog :title="title" :visible.sync="open" class="common-dialog" width="800px" append-to-body >
        <el-form ref="form" :model="form" :rules="rules" label-position="top">
          <el-row>
            <el-col :span="24">
              <el-form-item label="培训机构名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入培训机构名称" maxlength="30"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="负责人" prop="head">
                <el-input v-model="form.head" placeholder="请输入负责人" maxlength="30"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="电话号码" prop="phone">
                <el-input v-model="form.phone" placeholder="请输入电话号码" maxlength="30" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="地址" prop="address">
                <el-input v-model="form.address" placeholder="请输入地址" maxlength="30" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px"/>
        <!-- <el-form-item label="报事地点名称" prop="locationName">
          <el-input
            v-model="form.locationName"
            placeholder="请输入报事地点名称"
          />
        </el-form-item> -->
        <!-- <el-form-item label="地点名称" prop="placeName"> -->
          <!-- <el-input style="width:500px" v-model="form.placeName" placeholder="请输入地点名称" />
          <el-button @click="searchMap">搜索</el-button> -->
          <!-- <el-select
            style="width: 90%"
            @change="changeAddress"
            v-model="locationValue"
            filterable
            remote
            reserve-keyword
            placeholder="请输入地点名称"
            :remote-method="remoteMethod"
            :loading="loading"
          >
            <el-option
              v-for="item in locationList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item> -->
          <!-- 腾讯地图模块 -->
          <!-- <txMap
            ref="txMap"
            @postlocation="postlocation"
            @clickMap="clickMap"
            :longitude="form.longitude"
            :latitude="form.latitude"
          ></txMap> -->
        <!-- </el-form-item> -->
        <!-- <el-form-item label="经度" prop="longitude">
          <el-input
            v-model="form.longitude"
            disabled
            placeholder="请输入经度"
          />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" disabled placeholder="请输入纬度" />
        </el-form-item> -->
        <el-row>
            <el-col :span="9">
              <el-form-item label="简介图片" prop="image">
                <el-upload
                  :action="uploadAction"
                  list-type="picture-card"
                  :class="{hide:hideUpload}"
                  :file-list="uploadFiles"
                  :auto-upload="true"
                  :show-file-list="false"
                  :headers="uploadHeader"
                  :on-success="handleUploadSuccess">
                  <img
                    v-if="form.image"
                    :src="imagePath + form.image"
                    class="list-img"
                  />
                  <i v-if="!form.image" class="el-icon-plus"></i>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="9">
              <el-form-item label="logo" prop="image">
                <el-upload
                  :action="uploadAction"
                  list-type="picture-card"
                  :class="{hide:hideUpload}"
                  :file-list="uploadFiles"
                  :auto-upload="true"
                  :show-file-list="false"
                  :headers="uploadHeader"
                  :on-success="handleUploadSuccess">
                  <img
                    v-if="form.coverImage"
                    :src="imagePath + form.coverImage"
                    class="list-img"
                  />
                  <i v-if="!form.coverImage" class="el-icon-plus"></i>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="简介">
                <editor v-model="form.intro" :min-height="300"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确定</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { getToken } from '@/utils/auth';
  import txMap from './../Txmap/index.vue';
  import { getMtInstitutionList, saveSetting, update, deleteMtInstitution,getMtInstitutionInfor } from "@/api/mtInstitution";
  export default {
    name: "MerchantList",
    components: { txMap },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 标题
        title: "",
        // 选中数组
        ids: [],
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        list: [],
        // 是否显示弹出层
        open: false,
           // 搜索地点结果
        locationList: [],
        // 选中地址id
        locationValue: null,
        // 选中地址详情
        locationItem: {},
        // 默认排序
        defaultSort: {prop: 'createTime', order: 'descending'},
        // 商户类型
        typeOptions: [],
        // 表单参数
        form: {},
        // 上传地址
        uploadAction: process.env.VUE_APP_SERVER_URL + 'backendApi/file/upload',
        // 隐藏上传
        hideUpload: false,
        // 上传文件列表
        uploadFiles: [],
        uploadHeader: { 'Access-Token' : getToken() },
        // 图片根目录
        imagePath: "",
        // 查询参数
        queryParams: {
          page: 1,
          pageSize: 10,
          name: '',
        },
        // 表单校验
        rules: {

        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      // 查询日志
      getList() {
        this.loading = true;
        getMtInstitutionList(this.addDateRange(this.queryParams, this.dateRange)).then( response => {
            this.list = response.data.content;
            console.log(this.list,"this.list")
            this.total = response.data.totalElements;
            this.imagePath = response.data.imagePath;
            this.typeOptions = response.data.typeList;
            this.loading = false;
        });
      },
      // parseTime(createTimeArray){
      //   const createTime = new Date(createTimeArray[0], createTimeArray[1] - 1, createTimeArray[2], createTimeArray[3], createTimeArray[4], createTimeArray[5]);
      // return createTime.toLocaleString();
      // },
      // 搜索按钮操作
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
    //   // 点击地图修改标记
    // clickMap(evt) {
    //   console.log(evt, "子传父");
    //   // 经度
    //   this.form.longitude = evt.latLng.lng;
    //   // 维度
    //   this.form.latitude = evt.latLng.lat;
    //   if (evt.poi.name) {
    //     this.form.locationName = evt.poi.name;
    //     this.locationValue = evt.poi.name;
    //   }
    // },
    //   // 选择地址
    // changeAddress(id) {
    //   this.locationList.forEach((item) => {
    //     if (item.id == id) {
    //       this.locationItem = item;
    //     }
    //   });
    //   this.form.locationName = this.locationItem.title;
    //   this.form.managementPhone = this.locationItem.address;

    //   // console.log(id, "选中地址");
    //   // console.log(this.locationList, "locationList");
    //   // console.log(this.locationValue, "locationValue");
    //   console.log(this.locationItem, "locationItem");

    //   // 经度
    //   this.form.latitude = this.locationItem.location.lat;
    //   // 纬度
    //   this.form.longitude = this.locationItem.location.lng;
    //   // // // 调用地图组件传入经纬度,设置地图显示区域
    //   this.$refs["txMap"].setMap(this.form.longitude, this.form.latitude);
    //   // // 默认Marker为选中地址
    //   this.$refs["txMap"].setMapMarker(this.form.longitude, this.form.latitude);
    // },
    // // 地图组件给表单传搜索结果数据
    // postlocation(res) {
    //   this.locationList = res.data;
    //   console.log(this.locationList, "搜索结果");
    // },
    // // 模糊查询方法
    // remoteMethod(query) {
    //   if (query !== "") {
    //     this.query = query;
    //     // console.log(this.query,'父query');
    //     this.$refs["txMap"].getLal(query);
    //   } else {
    //     this.locationList = [];
    //   }
    // },
    //  // 设置地图
    //  setMap(placeName, longitude, latitude) {
    //   this.locationValue = placeName;
    //   this.$refs["txMap"].setMap(longitude, latitude);
    //   this.$refs["txMap"].setMapMarker(longitude, latitude);
    // },
    // // 重置地图
    // resetMap() {
    //   // // 调用地图组件传入经纬度,设置地图显示区域
    //   this.$refs["txMap"].setMap(104.244104, 30.589892);
    //   // 默认Marker为选中地址
    //   this.$refs["txMap"].setMapMarker(104.244104, 30.589892);
    //   this.locationValue = "";
    // },
      // 重置按钮操作
      resetQuery() {
        this.resetForm("queryForm");
        this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
        this.handleQuery();
      },
      // 状态修改
      handleStatusChange(row) {
        let text = row.status == "1" ? "启用" : "禁用";
        this.$modal.confirm('确认要' + text + '"' + row.name + '"商户吗？').then(function() {
           return updateMerchantStatus(row.id, row.status);
        }).then(() => {
           this.$modal.msgSuccess(text + "成功");
        }).catch(function() {
           row.status = row.status === "A" ? "A" : "D";
        });
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.multiple = !selection.length;
      },
      // 排序触发事件
      handleSortChange(column, prop, order) {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
        this.getList();
      },
      // 新增按钮操作
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "新增培训机构";
      },
      // 表单重置
      reset() {
        this.form = {
          id: "",
          name: "",
          address: "",
          phone: "",
          intro: "",
          image: "",
          coverImage: "",
          createTime: ""
        };
        this.resetForm("form");
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 提交按钮
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id) {
              update(this.form).then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                });
            } else {
              saveSetting(this.form).then(response => {
                  this.$modal.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                });
            }
          }
        });
      },
      // 修改按钮操作
      handleUpdate(row) {
        const app = this;
        app.reset();
        const id = row.id || this.ids;
        getMtInstitutionInfor(id).then(response => {
          app.form = response.data.mtInstitution;
          app.open = true;
          app.title = "编辑机构信息";
        });
      },
      // 删除按钮操作
      handleDelete(row) {
        const name = row.name || this.id;
        this.$modal.confirm('是否确认删除"' + name + '"的数据项？').then(function() {
          return deleteMtInstitution(row.id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      handleUploadSuccess(file) {
        this.form.logo = file.data.fileName;
      }
    }
  };
  </script>
  <style scoped>
  .common-dialog >>> .el-upload--picture-card {
    width: 60px;
    height: 50px;
    line-height: 60px;
  }
  </style>
  
  